<?php
namespace Home\Model;
use Think\Model;
use Common\Common\Classes\Message;

class StudentService extends Model{
    private $stu_sql;
    public function __construct(){
        $this->stu_sql = D("Student");
    }

    //---------------------------------根据学号获得某个学生的基本信息------------------------------------------
    public function getOneStuInfo($field){
        $stuInfo = $this->stu_sql->where($_POST)->field($field)->find();
        if($stuInfo!=""){
            $data = new Message(1,"查询到该学生!",$stuInfo);
        }
        else{
            $data = new Message(0,"查询不到该学生","");
        }
        return $data;
    }
    //---------------------------------检测学生登陆------------------------------------------
    public function check_login(){
        //修改
        $where["stu_email"] = I("post.number"); ;
        $where["stu_password_hash"] =  md5(I("post.password"));
        //
            $result = $this->stu_sql->where($where)->find();
            if ($result != "") {
                $_SESSION["name"] = $result['stu_name'];
                $_SESSION['isTeacher'] = "false";
                $_SESSION['number'] = $result["stu_email"];
                $_SESSION["id"] = $result["stu_id"];
                $data = new Message(1, "登陆成功,可以转换到主页!", "");
            } else {
                $data = new Message(0, "账号或者密码错误!", "");
            }
        return $data;
    }
    //---------------------------------学生注册------------------------------------------
    public function register(){
        if($this->stu_sql->create($_POST,1)){
            $_POST["email"] = $_POST["stu_email"];
            $tea_sql = new TeacherService();
            if($this->check_email()->status == 1 && $tea_sql->check_email()->status == 1 && $this->check_number()->status == 1){
                unset($_POST["email"]);
                $this->stu_sql->add($_POST);
                $data = new Message(1,"学生注册成功!","");
            }
            else{
                $data = new Message(0,"注册信息有误!","");
            }
        }
        else{
            $data = new Message(0,$this->stu_sql->getError(),"");
        }
        return $data;
    }
    //---------------------------------检测学生邮箱是否存在------------------------------------------
    public function check_email(){
        $where["stu_email"] = $_POST["email"];
        $result = $this->stu_sql->where($where)->find();
        if($result != ""){
            $data = new Message(0,"该邮箱已经被注册!","");
        }
        else{
            $data = new Message(1, "该邮箱可以使用!", "");
        }
        return $data;
    }
    //---------------------------------检测学生邮箱是否存在------------------------------------------
    public function check_number(){
        $where["stu_number"] = $_POST["stu_number"];
        $where["col_id"] = $_POST["col_id"];
        $result = $this->stu_sql->where($where)->find();
        if($result != ""){
            $data = new Message(0,"该学号已经被注册!","");
        }
        else{
            $data = new Message(1,"该学号可以使用!","");
        }
        return $data;
    }
    //---------------------------------获得登陆学生的基本信息------------------------------------------
    public function getInfo($field){
        $info = $this->stu_sql->relation("college")->field($field)->find($_SESSION["id"]);
        $info["state"] = "学生";
        return $info;
    }
    //---------------------------------获得某个学生的基本信息------------------------------------------
    public function getStuInfo($field){
        $where["stu_id"] = $_POST["stu_id"];
        $student = $this->stu_sql->where($where)->field($field)->find();
        return $student;
    }
    //---------------------------------更新学生基本信息------------------------------------------
    public function updateStudent(){
        if($this->stu_sql->create($_POST,2)){
            $where["stu_id"] = $_SESSION["id"];
            //修改
            $this->stu_sql->where($where)->save($_POST);
            $_SESSION["name"] = $_POST["stu_name"];
            $_SESSION["sex"] = $_POST["sex"];
            $data = new Message(1,"学生信息修改成功!","");
        }
        else{
            $data = new Message(0,$this->stu_sql->getError(),"");
        }
        return $data;
    }
    //---------------------------------更改学生的密码------------------------------------------
    public function updatePWD(){
        $where["stu_id"] = $_SESSION["id"];
        $where["stu_password_hash"] = $_POST["old_pwd"];
        $check = $this->stu_sql->where($where)->find();     //检测旧密码是否正确
        if($check != ""){
            $update["stu_password_hash"] = $_POST["new_pwd"];
            $result = $this->stu_sql->where($where)->save($update);     //修改
            if($result>0){
                $data = new Message(1,"密码修改成功!","");
            }
            else{           //失败的情况一般是:旧密码与新密码一样,没有修改到
                $data = new Message(0,"新密码与旧密码一样!","");
            }
        }
        else{
            $data = new Message(0,"旧密码错误!","");
        }
        return $data;
    }

    public function __destruct(){}
}